package com.ruoyi.web.controller.prevention;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.PreventionRiskOught;
import com.ruoyi.system.entity.PreventionRiskOughtFile;
import com.ruoyi.system.service.PreventionRiskOughtFileService;
import com.ruoyi.system.service.PreventionRiskOughtService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("preventionRiskOught")
public class PreventionRiskOughtController extends BaseController {

    @Resource
    private PreventionRiskOughtService preventionRiskOughtService;
    @Resource
    private PreventionRiskOughtFileService preventionRiskOughtFileService;

    /**
     * 分页查询
     *
     * @param preventionRiskOught 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(PreventionRiskOught preventionRiskOught) {
        startPage();
        List<PreventionRiskOught> riskOughtList = this.preventionRiskOughtService.queryByPage(preventionRiskOught);
        if (!CollectionUtils.isEmpty(riskOughtList)) {
            for (PreventionRiskOught promise : riskOughtList) {
                List<PreventionRiskOughtFile> promiseFiles = new ArrayList<>();
                String fileId = promise.getRiskFileId();
                if (StringUtils.isNotEmpty(fileId)) {
                    fileId = fileId.substring(0, fileId.length() - 1);
                    if (fileId.contains(",")) {
                        String[] split = fileId.split(",");
                        List<String> id = Arrays.asList(split);
                        if (StringUtils.isNotEmpty(id)) {
                            for (String s : id) {
                                PreventionRiskOughtFile riskOughtFile = preventionRiskOughtFileService.queryById(Integer.valueOf(s));
                                if (!ObjectUtils.isEmpty(riskOughtFile)) {
                                    promiseFiles.add(riskOughtFile);
                                }
                            }
                        }
                    } else {
                        PreventionRiskOughtFile riskOughtFile = preventionRiskOughtFileService.queryById(Integer.valueOf(fileId));
                        if (!ObjectUtils.isEmpty(riskOughtFile)) {
                            promiseFiles.add(riskOughtFile);
                        }
                    }
                }
                promise.setRiskOughtFileList(promiseFiles);
            }
        }
        return getDataTable(riskOughtList);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.preventionRiskOughtService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param preventionRiskOught 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody PreventionRiskOught preventionRiskOught) {
        return AjaxResult.success(this.preventionRiskOughtService.insert(preventionRiskOught));
    }

    /**
     * 编辑数据
     *
     * @param preventionRiskOught 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody PreventionRiskOught preventionRiskOught) {
        return AjaxResult.success(this.preventionRiskOughtService.update(preventionRiskOught));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.preventionRiskOughtService.deleteById(id));
    }

    /**
     * 文件上传
     *
     * @return 上传结果
     */
    @PostMapping("/fileUpload")
    public AjaxResult uploadAcceptance(@RequestBody PreventionRiskOught preventionRiskOught) {
        if (null == preventionRiskOught) {
            return AjaxResult.error("参数异常！");
        }
        if (null == preventionRiskOught.getId()) {
            return AjaxResult.error("参数异常！");
        }
        if (CollectionUtils.isEmpty(preventionRiskOught.getRiskOughtFileList())) {
            return AjaxResult.error("参数异常！");
        }
        StringBuilder key = new StringBuilder();
        if (!CollectionUtils.isEmpty(preventionRiskOught.getRiskOughtFileList())) {
            for (PreventionRiskOughtFile riskOughtFile : preventionRiskOught.getRiskOughtFileList()) {
                preventionRiskOughtFileService.insert(riskOughtFile);
                key.append(riskOughtFile.getId());
                key.append(",");
            }
        }
        if (!StringUtils.isEmpty(key.toString())) {
            PreventionRiskOught riskOught = new PreventionRiskOught();
            riskOught.setId(preventionRiskOught.getId());
            riskOught.setRiskFileId(key.toString());
            preventionRiskOughtService.update(riskOught);
        }
        return AjaxResult.success();
    }

    @Log(title = "风险应知卡(", businessType = BusinessType.EXPORT)
    @PostMapping("/exportData")
    public void export(HttpServletResponse response, @RequestBody Integer[] ids) {
        List<PreventionRiskOught> promiseList = new ArrayList<>();
        List<Integer> list = new ArrayList<>(Arrays.asList(ids));
        if (!CollectionUtils.isEmpty(list)) {
            for (Integer id : list) {
                PreventionRiskOught riskOught = preventionRiskOughtService.queryById(id);
                promiseList.add(riskOught);
            }
        }
        ExcelUtil<PreventionRiskOught> util = new ExcelUtil<>(PreventionRiskOught.class);
        util.exportExcel(response, promiseList, "风险应知卡(");
    }


    @GetMapping("export/pdf/{id}")
    public void exportPdf(@PathVariable("id") Integer id, HttpServletRequest request, HttpServletResponse response) {
        preventionRiskOughtService.fillPDFData(id, request, response);
    }


}

